package com.css.fxfzypg.modules.rating.expertRating.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.rating.expertRating.model.dto.SaveGlTaskScoreParam;
import com.css.fxfzypg.modules.rating.expertRating.model.vo.ExpertScoreDetailsVo;
import com.css.fxfzypg.modules.rating.expertRating.model.vo.KpRatingTaskByKrtIdVo;
import com.css.fxfzypg.modules.rating.expertRating.service.KpExpertRatingService;
import com.css.fxfzypg.modules.rating.task.model.entity.KpRatingTask;
import com.css.fxfzypg.modules.rating.task.model.vo.GlTaskScoreDetailsVo;
import com.css.fxfzypg.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 专家评分接口
 */
@Slf4j
@RequestMapping("kpExpertRating")
@RestController
public class KpExpertRatingController {
    @Autowired
    private KpExpertRatingService kpExpertRatingService;

    /**
     * 查询考评任务列表接口
     *
     * @return
     */
    @GetMapping("list")
    public RestResponse<List<KpRatingTask>> list() {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("用户id:{}", currUid);
            return RestResponse.succeed(kpExpertRatingService.list(currUid));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 查询考评任务下关联任务列表
     *
     * @return
     */
    @GetMapping("glTask/list")
    public RestResponse<KpRatingTaskByKrtIdVo> getKpRatingTaskByKrtId(@RequestParam String krtId) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("getKpRatingTaskByKrtId,参数:{}, 用户id:{}", krtId, currUid);
            return RestResponse.succeed(kpExpertRatingService.getKpRatingTaskByKrtId(krtId, currUid));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 查询关联任务得分详情
     *
     * @return
     */
    @GetMapping("glTask/details")
    public RestResponse<ExpertScoreDetailsVo> getGlTaskScoreDetails(@RequestParam String krtytId) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("查询关联任务得分详情,关联任务id:{}, currUid:{}", krtytId, currUid);
            return RestResponse.succeed(kpExpertRatingService.getGlTaskScoreDetails(krtytId, currUid));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 保存分数
     *
     * @param saveGlTaskScoreParams
     * @return
     */
    @PostMapping("glTask/save")
    public RestResponse<String> saveScore(@RequestBody SaveGlTaskScoreParam saveGlTaskScoreParams) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("param:{}", JSONObject.toJSONString(saveGlTaskScoreParams));
            kpExpertRatingService.saveGlTaskScore(saveGlTaskScoreParams, currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 提交接口
     *
     * @param krtytId id
     * @return
     */
    @GetMapping("glTask/commit")
    public RestResponse<String> commit(@RequestParam String krtytId) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("commit,krtytId:{}", krtytId);
            kpExpertRatingService.commit(krtytId, currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }
}
